package com.xhtranslate.service.baidu.controller;

import com.xhtranslate.common.dto.TextFormDTO;
import com.xhtranslate.common.dto.ImageFormDTO;
import com.xhtranslate.common.vo.ResponseEnum;
import com.xhtranslate.service.baidu.service.BaiduTranslateService;
import com.xhtranslate.common.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.controller
 * @date 2022/11/2 20:30:31
 * @description 接口
 */
@RestController
@Api(value = "百度翻译请求接口", tags = "百度翻译请求接口")
@RequestMapping(value = "baiduTranslate")
@Slf4j
public class BaiduTranslateController {
    @Autowired
    private BaiduTranslateService baiduTranslateService;

    @PostMapping(value = "commonTranslate")
    @ApiOperation(value = "通用翻译")
    public ResponseVO commonTranslate(@RequestBody @Validated TextFormDTO textFormDTO) {
        log.warn("通用翻译：" + textFormDTO);
        StringBuffer stringBuffer = baiduTranslateService.commonTranslate(textFormDTO);
        if ("".equals(stringBuffer) || StringUtils.isBlank(stringBuffer)) {
            return ResponseVO.exception(ResponseEnum.RESULTNULL);
        }
        return ResponseVO.succ(stringBuffer);
    }

    @PostMapping(value = "imageTranslate", consumes = "multipart/form-data")
    @ApiOperation(value = "图片翻译")
    public ResponseVO imageTranslate(@Validated ImageFormDTO dto) throws IOException {
        log.warn("图片翻译：" + dto);
        StringBuffer stringBuffer = baiduTranslateService.imageTranslate(dto);
        String string = stringBuffer.toString();
        if (string.equals(ResponseEnum.OVERCREDITLIMIT.getMsg())) {
            return ResponseVO.exception(ResponseEnum.OVERCREDITLIMIT);
        }
        if ("".equals(string) || StringUtils.isBlank(string)) {
            return ResponseVO.exception(ResponseEnum.RESULTNULL);
        }
        return ResponseVO.succ(stringBuffer);
    }
}

